package datastructures;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * A Window contains word. And has methods for setting,getting and calculating
 * attributes
 * 
 * @author Fredrik
 * 
 */
public class Window implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7717683701122839184L;

	private ArrayList<Word> words = new ArrayList<Word>();

	//private Coordinate windowCoordinate;

	/**
	 * Constructor creating a Window
	 * 
	 * @param cordinate
	 */
	public Window(Coordinate cordinate) {
//		this.windowCoordinate = cordinate;
	}

	/**
	 * Constructor that creates a window filled with words
	 * 
	 * @param words
	 * @param cordinate
	 */
	public Window(ArrayList<Word> words, Coordinate cordinate) {
//		this.windowCoordinate = cordinate;
		this.words = words;
	}

	/**
	 * Returns the coordinates of this window
	 * 
	 * @return the windows coordinates in the world
	 */
//	public Coordinate getWindowCoordinate() {
//		return this.windowCoordinate;
//	}

	/**
	 * Sets the windows coordinates
	 * 
	 * @param cordinates
	 */
//	public void setWindowCoordinate(Coordinate cordinates) {
//		this.windowCoordinate = cordinates;
//	}

	/**
	 * Get all the words in this window
	 * 
	 * @return all the words located in this window
	 */
	public ArrayList<Word> getWords() {
		return this.words;
	}

	/**
	 * Deletes a word from this window
	 * 
	 * @param word
	 * @return true if a word was successfully deleted
	 */
	public boolean deleteWord(Word word) {

		Word deletingWord = null;
		Iterator<Word> wordIterator = this.words.iterator();
		while (wordIterator.hasNext()) {
			Word aWord = wordIterator.next();
			if (aWord.equals(word)) {
				deletingWord = aWord;
				break;
			}
		}
		if (deletingWord != null) {
			if (deletingWord.deleteEdges()) {
				boolean a = this.words.remove(deletingWord);
				return a;
			}
			return false;

		}
		return false;

	}

	/**
	 * Adds a word to this window
	 * 
	 * @param w
	 * @return true if success, false if the word already existed in the window
	 */
	public boolean addWord(Word w) {
		if (!this.words.contains(w)) {
			this.words.add(w);
			return true;
		}
		return false;

	}

	/**
	 * Check if the word exist in this window
	 * @param w
	 * @return true if the word exist in this window
	 */
	public boolean wordExistInWindow(Word w) {
		return (!this.words.contains(w));
	}

	/**
	 * Guess at every word in this window
	 * @param guess
	 * @return true if any word matched the guess
	 */
	public boolean guessAtWord(String guess) {
		Iterator<Word> wordIterator = this.words.iterator();
		while (wordIterator.hasNext()) {
			Word nextWord = wordIterator.next();

			if (nextWord.isVisible() && nextWord.guess(guess)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Check if the window is visible in the play field
	 * @return true if the window should be visible
	 */
	public boolean isVisible() {
		Iterator<Word> wordsIterator = this.words.iterator();
		while (wordsIterator.hasNext()) {
			if (wordsIterator.next().isVisible()) {
				return true;
			}
		}
		return false;
	}

}
